package com.bracelet.controller;

import com.bracelet.beans.Location;
import com.bracelet.constant.ResponseCode;
import com.bracelet.socket.MySocket;
import com.bracelet.socket.SendMessageToClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

@Controller
public class SocketController {

    @Autowired
    private MySocket mySocket;

    private Socket socket;

    @ResponseBody
    @RequestMapping(value = "/getStatus", method = RequestMethod.POST)
    public void getStatus(){
        socket = mySocket.getSocket();
        if(socket == null){
            //error
            System.out.println("connected error");
            return;
        }
        System.out.println("isClosed: " + socket.isClosed());
        System.out.println("isConnected: " + socket.isConnected());
        //success
    }

    @ResponseBody
    @RequestMapping(value = "/getLocation", method = RequestMethod.POST)
    public Map<String, Object> getLocation(HttpServletRequest request, HttpServletResponse response){

        Map<String, Object> map = new HashMap<>();
        Location location = null;

        socket = mySocket.getSocket();

        if(socket == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            map.put("location", location);
            System.out.println("connected error");
            return map;
        }

        mySocket.sendMessage();

        SendMessageToClient sendMessageToClient = new SendMessageToClient();
        sendMessageToClient.start();

        try {
            sendMessageToClient.join(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String receiveMessage = MySocket.getReceiveMessage();
        String oldMessage = MySocket.getOldMessage();

        System.out.println("receiveMessage: " + receiveMessage);
        System.out.println("oldMessage: " + oldMessage);

        if((receiveMessage == null || receiveMessage.equals("")) && oldMessage != null && !oldMessage.equals("")){
            receiveMessage = oldMessage;
        }
        if(receiveMessage != null && !receiveMessage.equals("")){
            String[] message = receiveMessage.split(",");
            double lat = Double.parseDouble(message[0]);
            double log = Double.parseDouble(message[1]);
            location = new Location(lat, log);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }else{
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
        }
        map.put("location", location);
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            socket = null;
            return map;
        }
    }

}
